import pygame
import sys

# 初始化Pygame
pygame.init()

# 设置游戏窗口大小
WIDTH, HEIGHT = 450, 450
WINDOW_SIZE = (WIDTH, HEIGHT)
screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption("数独游戏")

# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (200, 200, 200)

# 定义数独初始局面（0表示空格）
initial_board = [
    [5, 3, 0, 0, 7, 0, 0, 0, 0],
    [6, 0, 0, 1, 9, 5, 0, 0, 0],
    [0, 9, 8, 0, 0, 0, 0, 6, 0],
    [8, 0, 0, 0, 6, 0, 0, 0, 3],
    [4, 0, 0, 8, 0, 3, 0, 0, 1],
    [7, 0, 0, 0, 2, 0, 0, 0, 6],
    [0, 6, 0, 0, 0, 0, 2, 8, 0],
    [0, 0, 0, 4, 1, 9, 0, 0, 5],
    [0, 0, 0, 0, 8, 0, 0, 7, 9]
]

# 定义字体
font = pygame.font.Font(None, 36)


# 定义游戏主循环
def main():
    board = [row[:] for row in initial_board]  # 创建数独局面的副本

    selected = None  # 选中的单元格
    running = True

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if selected:
                    if event.unicode.isdigit() and 1 <= int(event.unicode) <= 9 and initial_board[selected[0]][
                        selected[1]] == 0:
                        board[selected[0]][selected[1]] = int(event.unicode)
                elif event.key == pygame.K_RETURN:
                    if is_valid(board):
                        print("游戏胜利！")
                    else:
                        print("游戏失败，请检查数独规则！")

            elif event.type == pygame.MOUSEBUTTONDOWN:
                pos = pygame.mouse.get_pos()
                selected = (pos[1] // 50, pos[0] // 50)

        screen.fill(WHITE)
        draw_board(board, selected)
        pygame.display.flip()

    pygame.quit()
    sys.exit()


# 绘制数独面板
def draw_board(board, selected):
    for i in range(9):
        for j in range(9):
            rect = pygame.Rect(j * 50, i * 50, 50, 50)
            # 画棋盘颜色
            if (i // 3 + j // 3) % 2 == 0:
                pygame.draw.rect(screen, '#c8c8c8' if (i + j) % 2 == 0 else '#d8d8d8', rect)
            else:
                pygame.draw.rect(screen, '#f3f3f3' if (i + j) % 2 == 0 else WHITE, rect)
            # 画提示
            # if selected:
            #     pygame.draw.rect(screen, '#f3bc74', (selected[1] * 50, selected[0] * 50, 50, 50))
            # 画数字
            if board[i][j] != 0:
                font_color = '#000000'
                if initial_board[i][j] == board[i][j]:
                    font_color = '#000000'
                else:
                    font_color = '#6aab73'
                if selected and board[selected[0]][selected[1]] == board[i][j]:
                    # 如果是选中数字
                    font_color = '#ff0000'

                number = font.render(str(board[i][j]), True, font_color)
                screen.blit(number, (j * 50 + 20, i * 50 + 15))

    if selected:
        pygame.draw.rect(screen, (0, 128, 255), (selected[1] * 50, selected[0] * 50, 50, 50), 3)


# 检查数独是否合法
def is_valid(board):
    # 检查行和列
    for i in range(9):
        row = set()
        col = set()
        for j in range(9):
            if board[i][j] != 0 and board[i][j] in row:
                return False
            row.add(board[i][j])

            if board[j][i] != 0 and board[j][i] in col:
                return False
            col.add(board[j][i])

    # 检查3x3小方格
    for i in range(0, 9, 3):
        for j in range(0, 9, 3):
            square = set()
            for k in range(3):
                for l in range(3):
                    if board[i + k][j + l] != 0 and board[i + k][j + l] in square:
                        return False
                    square.add(board[i + k][j + l])

    return True


if __name__ == "__main__":
    main()
